package jmind.core.pdf;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.*;
import jmind.core.log.LogUtil;

import java.io.FileOutputStream;
import java.util.Collections;
import java.util.Map;

/**
 * description:给pdf文件加水印
 *
 * @author weibo.xie
 * @date : create in 10:52 上午 2021/8/6
 */
public class PdfWatermark{

    public static void main(String[] args) throws Exception {
        String path="/Users/weibo/Downloads/" ;
        jmind.core.pdf.PdfWatermark.waterMark(path+"out2.pdf",path+"out2_watermark.pdf","我是水印");
        // waterMark("D:\data\hello.pdf","D:\data\newHello.pdf","水印内容")
    }

    /**
     * 给pdf文件加水印
     * @param inputFile 源文件路径
     * @param outputFile 输出文件路径
     * @param waterMarkName 水印内容
     * @return
     */
    public static boolean waterMark(String inputFile, String outputFile, String waterMarkName){
        return  waterMark(inputFile, outputFile, waterMarkName,null);
    }
    public static boolean waterMark(String inputFile, String outputFile, String waterMarkName, Map<String,Float> properties) {
        try {
            if(properties==null){
                properties= Collections.emptyMap();
            }
            PdfReader reader = new PdfReader(inputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
            //这里的字体设置比较关键，这个设置是支持中文的写法
            BaseFont base = ChineseFont.BSAE_FONT;
            PdfContentByte under;
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
               // Rectangle   pageRect = stamper.getReader().getPageSizeWithRotation(i);
                // 计算水印X,Y坐标
                float x = properties.getOrDefault("x",290f);  //pageRect.getWidth() / 2;
                float y = properties.getOrDefault("y",400f);  //pageRect.getHeight() / 2;
                // 获得PDF最顶层
               under = stamper.getOverContent(i);//在内容上方加水印
               // under = stamper.getUnderContent(i);// 在内容下方加水印
                under.saveState();
                // set Transparency
                PdfGState gs = new PdfGState();
                // 设置透明度范围为0到1
                gs.setFillOpacity(properties.getOrDefault("fillOpacity",0.3f));
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(base,  properties.getOrDefault("fontAndSize",35f));//字体大小
                under.setColorFill(BaseColor.BLACK);//字体颜色
                // 水印文字成45度角倾斜
                under.showTextAligned(Element.ALIGN_CENTER, waterMarkName, x, y, properties.getOrDefault("rotation",45f));
                // 添加水印文字
                under.endText();
                under.setLineWidth(properties.getOrDefault("fillOpacity",1f));
                under.stroke();
            }
            stamper.close();
            reader.close();
            return true;
        } catch (Exception e) {
            LogUtil.error(PdfWatermark.class,e);
            return false;
        }
    }



}

